define(function(require, exports, module) {
	
	require("validator");
	
	$.fn.extend({
		/**
		 * 赋值
		 */
		setFormValue:function(data, model) {
			var values = null;
			var elems = null;
			var name = null;
			var temp = $("<div></div>");
			for(var key in data) {
				name = model ? model + "." + key : key;
				values = data[key];
				elems = $(this).find("[name='"+name+"']:radio");
				if(elems.length > 0) {
					var value = values + "";
					elems.each(function() {
						this.checked = $(this).attr("value") == value;
					});
				} else {
					elems = $(this).find("[name='"+name+"']:checkbox");
					if(elems.length > 0) {
						if(!$.isArray(values)) {
							var value = values;
							values = [];
							if(value == true) {
								values.push("1");
							} else if(value == false) {
								values.push("0");	
							} else {
								values.push(value + "");
							}
						}
						elems.each(function() {
							this.checked = values.indexOf($(this).attr("value")) > -1;
						});
					} else {
						elems = $(this).find("[name='"+name+"']");
						if(typeof(values) == "string") {
							values = temp.html(values).text();
						}
						elems.val(values);
					}
				}
			}
		},
		ajaxSubmit:function(callback) {
			if($(this).get(0).tagName.toUpperCase() == "FORM") {
				var me = $(this).get(0);
				var _this = $(me);
				if(!_this.data("submiting")) {
					_this.data("submiting", true);
					var enctype = _this.attr("enctype");
					//上传模式
					if(enctype) {
						var formData = new FormData(me);
						var url = _this.attr("action");
						$.ajax({  
			                type: 'post',  
			                url: url,  
			                data: formData,  
			                cache: false,  
			                processData: false,
			                contentType: false,
			                success: function(result) {  
				            	if(typeof(callback) == "function") {
									callback.call(me, result);
								}
				            	_this.data("submiting", false);
				            }
			            }); 
					} else {
						var url = _this.attr("action");
						var params = _this.serialize();
						$.post(url, params, function(result) {
							if(typeof(callback) == "function") {
								callback.call(me, result);
							}
							_this.data("submiting", false);
						}, "json");
					}
				}
			}
		}
	});
	
	module.exports = {};

});